package cn.z2huo.datastructure.linearlist.doublelinkedlist;

import cn.z2huo.datastructure.linearlist.doublelinkedlist.entity.Node;

/**
 * 双向链表删除某一个元素的操作
 * 作者：张一卓
 * 创建日期：2020/9/27
 */
public class ListDelete {
    public static void main(String[] args) {
        Node<Integer> root = DoubleLinkedListInit.integerInit();
        DoubleLinkedListUtils.printDoubleLinkedList(root);
        delete(root, 3);
        DoubleLinkedListUtils.printDoubleLinkedList(root);
    }

    public static <T> void delete(Node<T> root, int index){
        Node<T> precursor = root;
        // h 0 1 2 3 4
        for (int i = 0; i < index; i++){
            precursor = precursor.getNext();
        }
        precursor.setNext(precursor.getNext().getNext());
        precursor.getNext().setPrior(precursor);
    }
}
